﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DataLoad.iContact.WinClient.Properties;

namespace DataLoad.iContact.WinClient
{
    public partial class frmLargeText : Form
    {
        #if !MONO
        [DllImport("user32", SetLastError = true, EntryPoint = "SetWindowPos")]
        static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
        #else
        static int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags)
        {
            return 0;
        }
        #endif

        private const int HWND_TOPMOST = -1;
        private const int HWND_NOTOPMOST = -2;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

        public void MakeNormal(int hwnd)
        {
            SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
        }
        public void MakeTopMost(int hwnd)
        {
            SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
        }

        private frmMain _mainForm;

        public frmLargeText(frmMain mainForm)
        {
            InitializeComponent();

            _mainForm = mainForm;
        }

        private static Font GetAdjustedFont(Graphics GraphicRef, string GraphicString, Font OriginalFont, int ContainerWidth, int MaxFontSize, int MinFontSize, bool SmallestOnFail)
        {
            // We utilize MeasureString which we get via a control instance           
            for (int AdjustedSize = MaxFontSize; AdjustedSize >= MinFontSize; AdjustedSize--)
            {
                Font TestFont = new Font(OriginalFont.Name, AdjustedSize, OriginalFont.Style);

                // Test the string with the new size
                SizeF AdjustedSizeNew = GraphicRef.MeasureString(GraphicString, TestFont);

                if (ContainerWidth > Convert.ToInt32(AdjustedSizeNew.Width))
                {
                    // Good font, return it
                    return TestFont;
                }
            }

            // If you get here there was no fontsize that worked
            // return MinimumSize or Original?
            if (SmallestOnFail)
            {
                return new Font(OriginalFont.Name, MinFontSize, OriginalFont.Style);
            }
            else
            {
                return OriginalFont;
            }
        }

        private bool _dial = false;

        public static void ShowLargeText(string text)
        {
            using (frmLargeText frm = new frmLargeText((frmMain)Form.ActiveForm))
            {
                int width = Screen.FromControl(frm).WorkingArea.Width;

                int leftPad = (width * 5 / 100);
                width -= leftPad;
                using (Graphics g = frm.CreateGraphics())
                {
                    Font font = GetAdjustedFont(g, text, frm.lblText.Font, width, 100, 10, true);

                    SizeF textSize = g.MeasureString(text, font);

                    frm.SetClientSizeCore((int)textSize.Width, (int)textSize.Height);

                    frm.lblText.Top = 0;
                    frm.lblText.Left = 0;
                    frm.lblText.Font = font;
                    frm.lblText.Text = text;

                    frm.StartPosition = FormStartPosition.CenterScreen;

                    frm.Opacity = 0.8;

                    frm.ShowDialog();

                    if (frm._dial)
                    {
                        frm._mainForm.DialWithSkype(text);
                    }
                }
            }
        }

        private void frmLargeText_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmLargeText_Load(object sender, EventArgs e)
        {
            MakeTopMost((int)this.Handle);
        }

        private void lblSkype_Click(object sender, EventArgs e)
        {
            _dial = true;
            Close();
        }

        private void picClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
